//树状数组-Promotion Counting P
//思路: dfs的过程中维护权值数组
//从某个节点进入时: ret[i] -= query(n) - query[p[i]]
//从这个节点出去时: ret[i] += query(n) - query[p[i]]
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
#define lowbit(x) (x & -x)
typedef long long ll;
const int N = 1e5 + 10;
ll p[N], t[N]; //能力值
int n;
ll s[N]; //树状数组

vector<int> edges[N]; //存树
ll ret[N]; //结果
unordered_map<int, int> mp; //离散化

void modify(int x, ll k)
{
    for(int i = x; i <= n; i += lowbit(i)) 
        s[i] += k;
}

ll query(int x)
{
    ll sum = 0;
    for(int i = x; i; i -= lowbit(i)) sum += s[i];
    return sum;
}

void dfs(int x)
{
    ret[x] -= query(n) - query(p[x]);
    for(auto y : edges[x]) dfs(y);
    ret[x] += query(n) - query(p[x]);
    modify(p[x], 1);
}

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++) 
    {
        cin >> p[i];
        t[i] = p[i];
    }
    for(int i = 2; i <= n; i++)
    {
        int fa; cin >> fa;
        edges[fa].push_back(i);
    }

    //离散化
    sort(t + 1, t + 1 + n);
    for(int i = 1; i <= n; i++) mp[t[i]] = i;
    for(int i = 1; i <= n; i++) p[i] = mp[p[i]];
    dfs(1);
    for(int i = 1; i <= n; i++) cout << ret[i] << endl;
    return 0;
}